﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;

namespace lchealthy
{
    public partial class frmshop : Form
    {
        string file = "";
        DataSet dsfen;
        public frmshop()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "图片|*.jpg";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                frmadmin f = this.MdiParent as frmadmin;
                file = openFileDialog1.FileName;
                if (File.Exists(Application.StartupPath + "\\bg.jpg"))
                {                    
                    f.frmbgc();
                    File.Delete(Application.StartupPath + "\\bg.jpg");
                }
                File.Copy(file, Application.StartupPath + "\\bg.jpg");
                MessageBox.Show("设置成功！");                
                f.frmbg();

            }
        }

       

        private void btncancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string name = txtName.Text.Trim();
            string add = txtadd.Text.Trim();
            string phone = txtPhone.Text.Trim();
            string sql = "select top 1 sid from shopinfo";
            object b = DbHelperOleDb.GetSingle(sql);
            if (b != null)
            {
                sql = string.Format("update shopinfo set sname='{0}',stel='{1}',saddress='{2}' where sid={3}", name, phone, add, int.Parse(b.ToString()));
                DbHelperOleDb.ExecuteSql(sql);
                MessageBox.Show("更新成功！");
                shopinfo.name = name;
                shopinfo.add = add;
                frmadmin f = this.MdiParent as frmadmin;
                f.setstatus();
                this.Close();
            }            
        }

        private void frmshop_Load(object sender, EventArgs e)
        {
            DataSet ds = null;
            string sql = "select top 1 sid from shopinfo";
            object b = DbHelperOleDb.GetSingle(sql);
            if (b != null)
            {
                sql = "select sname,stel,saddress from shopinfo where sid="+int.Parse(b.ToString());
                ds= DbHelperOleDb.Query(sql);
                txtName.Text = ds.Tables[0].Rows[0][0].ToString();
                txtPhone.Text = ds.Tables[0].Rows[0][1].ToString();
                txtadd.Text = ds.Tables[0].Rows[0][2].ToString();    

            }
            dgvfenbind();
            
        }

        private void dgvfenbind()
        {
            string sql = "select id,jifentext,jnum from jifenconfig";
            dsfen = DbHelperOleDb.Query(sql);
            dgvfen.DataSource = dsfen.Tables[0];            
        }

        private void btnfen_Click(object sender, EventArgs e)
        {
            OleDbDataAdapter da = new OleDbDataAdapter("select id,jifentext,jnum from jifenconfig", new OleDbConnection(DbHelperOleDb.connectionString));
            OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
            da.UpdateCommand = cb.GetUpdateCommand();
            da.InsertCommand = cb.GetInsertCommand();
            da.DeleteCommand = cb.GetDeleteCommand();
            //dsfen.Tables[0].AcceptChanges();
            da.Update(dsfen, dsfen.Tables[0].TableName);
            MessageBox.Show("更新成功！");
           dgvfenbind();
        }
    }
}
